package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.ruoyi.common.enums.ProjectTaskStatus;
import com.ruoyi.common.enums.ProjectTaskType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BProjectTask;
import com.ruoyi.system.domain.BQuestion;
import com.ruoyi.system.domain.request.ProjectTaskRequest;
import com.ruoyi.system.domain.vo.BProjectTaskVo;
import com.ruoyi.system.domain.vo.BScoreRuleVo;
import com.ruoyi.system.domain.vo.BUserVo;
import com.ruoyi.system.service.BProjectTaskService;
import com.ruoyi.system.mapper.BProjectTaskMapper;
import com.ruoyi.system.service.BQuestionService;
import com.ruoyi.system.service.BTaskUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author yangxingze
* @description 针对表【b_project_task(项目任务表)】的数据库操作Service实现
* @createDate 2024-12-28 00:45:04
*/
@Service
public class BProjectTaskServiceImpl extends ServiceImpl<BProjectTaskMapper, BProjectTask>
    implements BProjectTaskService{

    @Autowired
    BQuestionService questionService;

    @Autowired
    BTaskUserService taskUserService;

    /**
     * 获取我的任务列表
     * @param request
     * @return
     */
    @Override
    public List<BProjectTaskVo> getTaskList(ProjectTaskRequest request) {
        String type = request.getType();
        List<BProjectTaskVo> taskList = null;
        if(type.equals(ProjectTaskType.ALL.getValue())){//所有
            taskList = baseMapper.selectTaskListByAll(request);
        }
        if(type.equals(ProjectTaskType.INCOMPLETE.getValue())){//未完成
            taskList = baseMapper.selectTaskListByIncomplete(request);
        }
        if(type.equals(ProjectTaskType.COMPLETE.getValue())){//已完成
            taskList = baseMapper.selectTaskListByComplete(request);
        }
        if(type.equals(ProjectTaskType.REJECT.getValue())){//被拒绝
            taskList = baseMapper.selectTaskListByReject(request);
        }
        if(ObjectUtils.isNotEmpty(taskList)){
            for(BProjectTaskVo task : taskList){
                String taskType = task.getType();
                String taskStatus = task.getStatus();
                if(taskType.equals("0")){//训练项目
                    if(taskStatus.equals(ProjectTaskStatus.INCOMPLETE.getValue())){
                        task.setTaskStatus(ProjectTaskType.INCOMPLETE.getValue());
                    }
                    if(taskStatus.equals(ProjectTaskStatus.COMPLETE.getValue())
                            || taskStatus.equals(ProjectTaskStatus.AUDIT_PASS.getValue())){
                        task.setTaskStatus(ProjectTaskType.COMPLETE.getValue());
                    }
                    if(taskStatus.equals(ProjectTaskStatus.REJECT.getValue())){
                        task.setTaskStatus(ProjectTaskType.REJECT.getValue());
                    }
                }else{//审核项目
                    if(taskStatus.equals(ProjectTaskStatus.COMPLETE.getValue())
                            || taskStatus.equals(ProjectTaskStatus.INCOMPLETE.getValue())){
                        task.setTaskStatus(ProjectTaskType.INCOMPLETE.getValue());
                    }
                    if(taskStatus.equals(ProjectTaskStatus.AUDIT_PASS.getValue())
                            || taskStatus.equals(ProjectTaskStatus.REJECT.getValue())){
                        task.setTaskStatus(ProjectTaskType.COMPLETE.getValue());
                    }
                }
            }
        }
        return taskList;
    }

    /**
     * 获取用户列表
     * @param projectId
     * @param type
     * @return
     */
    @Override
    public List<BUserVo> selectUserListByProjectId(Long projectId, String type) {
        return baseMapper.selectUserListByProjectId(projectId,type);
    }

    /**
     * 根据任务Id获取详细信息
     * @param taskId
     * @return
     */
    @Override
    public BProjectTaskVo getInfo(Long taskId) {
        Long userId = SecurityUtils.getUserId();
        BProjectTaskVo task = baseMapper.getInfo(taskId,userId);
        return task;
    }

    /**
     * 新增项目任务
     * @param projectTaskVo
     * @return
     */
    @Override
    public boolean saveVo(BProjectTaskVo projectTaskVo) {
        BQuestion bQuestion = questionService.getById(projectTaskVo.getQuestionId());
        BProjectTask projectTask = new BProjectTask();
        BeanUtils.copyProperties(projectTaskVo, projectTask);
        projectTask.setContent(bQuestion.getContent());
        projectTask.setCreateTime(new Date());
        boolean result = this.save(projectTask);
        return result;
    }

    /**
     * 修改项目任务
     * @param projectTaskVo
     * @return
     */
    @Override
    public boolean updateVo(BProjectTaskVo projectTaskVo) {
        BProjectTask projectTask = this.getById(projectTaskVo.getId());
        if(ObjectUtils.isEmpty(projectTask)){
            throw new ServiceException("项目任务不存在");
        }
        String status = projectTask.getStatus();
        if(!status.equals(ProjectTaskStatus.INCOMPLETE.getValue())){
            throw new ServiceException("项目任务进行中，无法修改");
        }
        BQuestion bQuestion = questionService.getById(projectTaskVo.getQuestionId());
        BeanUtils.copyProperties(projectTaskVo, projectTask);
        projectTask.setContent(bQuestion.getContent());
        projectTask.setUpdateTime(new Date());
        boolean result = this.updateById(projectTask);
        return result;
    }

    /**
     * 获取项目任务列表
     * @param projectTask
     * @return
     */
    @Override
    public List<BProjectTaskVo> selectTaskList(BProjectTask projectTask) {
        return baseMapper.selectTaskList(projectTask);
    }

    /**
     * 查询总数
     * @param projectTask
     * @return
     */
    @Override
    public long countByQueryParam(BProjectTask projectTask) {
        return baseMapper.countByQueryParam(projectTask);
    }

    /**
     * 导出我的任务列表
     * @param projectTask
     * @return
     */
    @Override
    public List<BProjectTaskVo> exportTaskList(BProjectTask projectTask) {
        List<BProjectTaskVo> list = baseMapper.exportTaskList(projectTask);
        for(BProjectTaskVo task : list){
            StringBuffer scoreString = new StringBuffer();
            StringBuffer contentString = new StringBuffer();
            String status = task.getStatus();
            if(!status.equals(ProjectTaskStatus.INCOMPLETE.getValue())){
                List<BScoreRuleVo> scoreRuleList = task.getScoreRuleList();
                for(BScoreRuleVo score : scoreRuleList){
                    scoreString.append(score.getName());
                    scoreString.append("：");
                    scoreString.append(score.getScore());
                    scoreString.append("；");
                }
            }
            Map<String,Object> detail = task.getDetails();
            if(ObjectUtils.isNotEmpty(detail)){
                for(Map.Entry<String, Object> entry : detail.entrySet()){
                    contentString.append(entry.getKey());
                    contentString.append("：");
                    contentString.append(entry.getValue());
                    contentString.append("；");
                }
            }
            task.setScoreString(scoreString.toString());
            task.setContentString(contentString.toString());
        }

        return list;
    }

    /**
     * 根据id查询项目任务
     * @param id
     * @return
     */
    @Override
    public BProjectTaskVo selectTaskById(Long id) {
        return baseMapper.selectTaskById(id);
    }

    /**
     * 删除项目任务
     * @param ids
     * @return
     */
    @Override
    public int deleteTaskByIds(Long[] ids) {
        return baseMapper.deleteTaskByIds(ids);
    }
}




